package com.jifen.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import util.TotalClass;
import util.UtilDAO;

import com.AddressListModule.bean.KehujifenBean;
import com.jifen.bean.JiFen;

public class JifenDao {

	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-4-6
	 *上午07:49:09
	 *zzClub
	 *com.jifen.dao
	 */
	private String sql = "";
	private ResultSet rs = null;
	JiFen jiFen = null;
	/**
	 * 
	 *@author qingyu zhang
	 *@function:设置点播积分
	 * @param jf
	 * @return
	 *2011-4-6
	 *上午09:32:55
	 *zzClub
	 *com.jifen.dao
	 *boolean
	 */
	public boolean addJiFen(JiFen jf){
		System.out.println("得到ID="+jf.getTypeId());
		boolean bo = false;
		String condition=" typeId='"+jf.getTypeId()+"'and integralType="+jf.getIntegralType();
		JiFen jiFen = getJiFen("tb_integral","setTime",condition);
		if(jiFen==null){
			sql="insert into tb_integral (typeId,userId,integralValue,integralType,setTime,seriseId) " +
			"values('"+jf.getTypeId()+"',"+jf.getUserId()+",'"+jf.getIntegralValue()+"',"+jf.getIntegralType()+",'"+jf.getSetTime()+"','"+jf.getSeriseId()+"')";
		}else{
			sql="update tb_integral set integralValue='"+jf.getIntegralValue()+"',setTime='"+jf.getSetTime()+"' where typeId='"+jf.getTypeId()+"' and integralType="+jf.getIntegralType();
		}
		
		System.out.println("插入语句"+sql);
		bo =  new TotalClass().operatorObject(sql);
		return bo;
	}
	public boolean addJiFenXilie(JiFen jf){
		System.out.println("得到ID="+jf.getTypeId());
		boolean bo = false;
		String condition=" seriseId='"+jf.getSeriseId()+"'";
		JiFen jiFen = getJiFen("tb_integral","setTime",condition);
		if(jiFen==null){
			sql="insert into tb_integral (userId,integralValue,integralType,setTime,seriseId) " +
			"values("+jf.getUserId()+",'"+jf.getIntegralValue()+"',"+jf.getIntegralType()+",'"+jf.getSetTime()+"','"+jf.getSeriseId()+"')";
		}else{
			sql="update tb_integral set integralValue='"+jf.getIntegralValue()+"',setTime='"+jf.getSetTime()+"' where seriseId='"+jf.getSeriseId()+"'";
		}
		
		System.out.println("插入语句"+sql);
		bo =  new TotalClass().operatorObject(sql);
		return bo;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据typeId得到积分对象
	 * @param table
	 * @param order
	 * @param conditions
	 * @return
	 *2011-4-6
	 *上午07:53:22
	 *zzClub
	 *com.jifen.dao
	 *JiFen
	 */
	public JiFen getJiFen(String table,String order,String condition){
		sql= UtilDAO.getList(table, order, condition);
		System.out.println("修改积分语句"+sql);
		ResultSet rs =  new TotalClass().getResult(sql);
		try {
			if(rs.next()){
				if(jiFen==null){
					jiFen = new JiFen();
				}
				jiFen = getJiFen(rs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jiFen;
	}
	public JiFen getJiFen(ResultSet rs){
		JiFen jf = new JiFen();
		try {
			jf.setIntegralId(rs.getInt("integralId"));
			jf.setIntegralType(rs.getInt("integralType"));
			jf.setIntegralValue(rs.getString("integralValue"));
			jf.setSeriseId(rs.getString("seriseId"));
			jf.setSetTime(rs.getString("setTime"));
			jf.setTypeId(rs.getString("typeId"));
			jf.setUserId(rs.getInt("userId"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jf;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据手机号码得到此号码的积分
	 * @param phone
	 * @return
	 *2011-4-15
	 *上午11:00:27
	 *zzClub
	 *com.jifen.dao
	 *KehujifenBean
	 */
	public KehujifenBean getKehujifen(String phone){
		sql="select * from tb_kehujifen where customerPhone='"+phone+"'";
		rs = new TotalClass().getResult(sql);
		KehujifenBean kehujifen = null;
		try {
			while(rs.next()){
				if(kehujifen==null){
					kehujifen =new KehujifenBean();
				}
				kehujifen.setCustomerId(rs.getInt("customerId"));
				kehujifen.setCustomerIntegral(rs.getString("customerIntegral"));
				kehujifen.setCustomerName(rs.getString("customerName"));
				kehujifen.setCustomerPhone(rs.getString("customerPhone"));
				kehujifen.setLastTime(rs.getString("lastTime"));
				kehujifen.setDirId(rs.getInt("dirId"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return kehujifen;
	}
}
